import {
  Component, Input,
  OnInit,
} from '@angular/core';
import { ApesMessageBaseService, ApesMessageService } from '@shared/apes/message';
import { ApiService } from '@core/services/http/api.service';
import { UserAuthService } from '@core/services/user-auth.service';
import { FormService } from '@core/services/form.service';
import set = Reflect.set;

@Component({
  selector: 'apes-todo-summary',
  templateUrl: './todoSummary.component.html',
  styleUrls: ['./todoSummary.component.scss'],
})
export class TodoSummaryComponent implements OnInit {

  toDoTypeItem: any[] = [
    { name: '经办', checked: false },
    { name: '督办', checked: false },
    { name: '发起', checked: false },
  ];

  @Input() department: any = ''; // TEST001 ECC001
  @Input() toDoType: any = '经办';
  @Input() init: boolean = true;
  @Input()
  set toDoConfig(value: any) {
    this.config = value;
  }

  get toDoConfig() {
    return this.config;
  }

  @Input()
  set searchData(value: boolean) {
    if (value) {
      this.getTodoList();
    }
    this.search = value;
  }

  get searchData() {
    return this.search;
  }

  isLoading: boolean = false;
  search: boolean = false;
  config: any;

  openDrawer: boolean = false;
  drawerData: any[] = [];

  toDoList: any[] = [
    // {
    //   documentType: '服务方案',
    //   list: [
    //     {
    //       statusName: '待提交方案',
    //       summary: 1000,
    //       timeOut: 70,
    //       warning: 40,
    //     }
    //   ],
    // }
  ];

  constructor(
    private msg: ApesMessageService,
    private formService: FormService,
    private apiService: ApiService,
  ) {
  }

  ngOnInit() {
    if(this.init){
      this.getTodoList();
    }
  }

  checkChange(type) {
    this.toDoType = type;
    this.getTodoList();
  }

  checkDtailsList(list, item) {
    // console.log(list);
    let statusName = item.statusName;
    let sendData = {
      'toDoType': this.toDoType,
      'documentType': list.documentType,
      'statusName': statusName,
    };
    if(this.department == ''){
      sendData['dept_id'] = this.config['dept_id'];
    }else {
      sendData['enumeration_type_id'] = this.department
    }
    if(sendData.hasOwnProperty('dept_id') && sendData['dept_id'] == null){
      this.msg.error('请选择所属门店！');
      return;
    }

    this.apiService.post('crm.customerControl.findByDocumentType', { 'body': JSON.stringify(sendData) })
      .subscribe((body) => {
        if (body && body.length > 0) {
          console.log(body);
          this.openDrawer = true;
          this.drawerData = [];
          body.forEach((item) => {
            item['todoNode'] = statusName;
            if (item['expired']) {
              item['expiredTime'] = item['expired'];
            }
            if (item.hasOwnProperty('content')) {
              item['carLicense'] = item.content['车牌'];
              item['contentData'] = [];
              for (let key in item.content) {
                if (key != '车牌') {
                  item['contentData'].push(key + ':' + item.content[key]);
                }
              }
            }
          });
          this.drawerData = [...body];
        }
      });
  }

  closeDrawer() {
    this.openDrawer = false;
  }

  getTodoList() {
    this.toDoTypeItem.forEach((item) => {
      item.checked = item.name == this.toDoType;
    });

    // 6194R001  1001040100  ECC001
    let sendData = {
      'toDoType': this.toDoType,
    };
    if(this.department == ''){
      sendData['dept_id'] = this.config['dept_id'];
    }else {
      sendData['enumeration_type_id'] = this.department
    }
    if(sendData.hasOwnProperty('dept_id') && sendData['dept_id'] == null){
      this.msg.error('请选择所属门店！');
      return;
    }

    this.isLoading = true;
    this.apiService.post('crm.customerControl.findAll', { 'body': JSON.stringify(sendData) })
      .subscribe((body) => {
        if (body) {
          this.toDoList = body;
        }
        this.isLoading = false;
      });
  }

  selectData(item) {
    this.openDrawer = false;
    setTimeout(() => {
      if (item.hasOwnProperty('tags')) {
        item['OAInterface'] = false;
        item['id'] = item['businessKey'];
        if (item.hasOwnProperty('formId')) {
          this.formService.navigateToForm(item);
        } else {
          this.msg.error('该待办未配置具体页面！');
        }
      } else {
        this.formService.navigateToForm(item);
      }
    }, 500);
  }

  showTimeClass(endTime) {
    let status = '';
    if (endTime != null && endTime != '' && endTime != undefined) {
      let nowTime = new Date().getTime();
      if (endTime < nowTime) {
        status = 'error';
      } else {
        status = 'success';
      }
    }
    return status;
  }

  showCountDown(endTime) {
    let showTime = '';
    if (endTime != null && endTime != '' && endTime != undefined) {
      let nowTime = new Date().getTime();
      let countDown;
      if (endTime > nowTime) {
        countDown = endTime - nowTime;
        showTime = '';
      } else {
        countDown = nowTime - endTime;
        showTime = '-';
      }
      let day = Math.floor(countDown / (1000 * 60 * 60 * 24));  //计算天数
      let hour = Math.floor(countDown / (1000 * 60 * 60) % 24);  //计算小时数
      let minute = Math.floor(countDown / (1000 * 60) % 60);  //计算分钟数
      let second = Math.floor(countDown / 1000 % 60);  //计算秒数
      let totalHours;
      let totalMinute;
      let totalSecond;
      if (day > 0) {
        totalHours = (day * 24 + hour);
      } else {
        if (hour < 10) {
          totalHours = '0' + hour;
        } else {
          totalHours = hour;
        }
      }
      if (minute < 10) {
        totalMinute = '0' + minute;
      } else {
        totalMinute = minute;
      }
      if (second < 10) {
        totalSecond = '0' + second;
      } else {
        totalSecond = second;
      }

      showTime += totalHours + ':' + totalMinute + ':' + totalSecond;
    } else {
      showTime = '';
    }

    return showTime;
  }


}
